package vista;

import java.awt.Component;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;

public class ButtonEditor extends DefaultCellEditor
{
	private static final long serialVersionUID = 1L;
	protected JButton button;
	private ActionListener actionListener;

	public ButtonEditor(ActionListener actionListener)
	{
		super(new JCheckBox());
		button = new JButton();
		button.setOpaque(true);
		button.setFont(new java.awt.Font("Tahoma", 0, 10));
		this.actionListener = actionListener;
	}
	
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
	{
		if (isSelected)
		{
			button.setForeground(table.getSelectionForeground());
			button.setBackground(table.getSelectionBackground());
		}
		else
		{
			button.setForeground(table.getForeground());
			button.setBackground(table.getBackground());
		}

		String label = ((JButton) value).getText();
		button.setText(label);
		button.setFont(new java.awt.Font("Tahoma", 0, 10));
		button.setActionCommand(((JButton) value).getActionCommand());
		if (button.getActionListeners().length == 0)
		{
			button.addActionListener(actionListener);
		}
		return button;
	}
}
